package com.gullmall.search.controller;

import com.gullmall.common.exception.GullmallCodeEnum;
import com.gullmall.common.to.es.SkuEsModel;
import com.gullmall.common.utils.R;
import com.gullmall.search.service.ProductSaveService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("search/save")
public class ElasticSaveController {

    @Resource(name = "productSaveServiceImpl")
    private ProductSaveService productSaveService;

    //上架商品
    @PostMapping("/product")
    public R productStatusUp(@RequestBody List<SkuEsModel> skuEsModels){
        boolean allDone=true; //批量插入全部成功
        try {
            allDone=productSaveService.productStatusUp(skuEsModels);
        } catch (IOException e) {
            e.printStackTrace();
            return R.error(GullmallCodeEnum.PRODUCT_UP_EXCEPTION.getCode(),"商品上架异常");
        }
        return allDone?R.ok(): R.error(GullmallCodeEnum.PRODUCT_UP_EXCEPTION.getCode(),"某个sku上架失败");
    }
}
